package com.imooc.course.file.controller.ablum;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetMezzanineInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.imooc.course.file.form.FileForm;
import com.imooc.course.server.dto.FileDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.enums.FileUseEnum;
import com.imooc.course.server.service.FileService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.VodUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;

@RestController
@RequestMapping("/album/player")
public class AlbumPlayerController {

    private static final Logger log = LoggerFactory.getLogger(AlbumPlayerController.class);
    public static final String BUSINESS_NAME = "Album Player";

    @Value("${vod.regionId}")
    private String regionId;

    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    @Value("${vod.accessKeyId}")
    private String accessKeyId;//子RAM用户accessKeyId

    @Value("${vod.accessKeySecret}")
    private String accessKeySecret;//子RAM用户accessKeySecret

    @Value("${vod.cateId}")
    private Long cateId;

    @Value("${vod.templateGroupId}")
    private String templateGroupId;

    @Resource(name = "fileService")
    private FileService fileService;

    @PostMapping("/get-auth")
    public ResponseDto getAuthByVod(String videoId) {
        VodUtil vodUtil = new VodUtil();
        GetVideoPlayAuthResponse response;
        try {
            response = vodUtil.getVideoPlayAuth(this.createClient(vodUtil),videoId);
        } catch (Exception e) {
            log.error("无法获取视频点播服务凭证",e);
            e.printStackTrace();
            return ResponseDto.fail();
        }
        return ResponseDto.success().setContent(response.getPlayAuth());
    }

    private DefaultAcsClient createClient(VodUtil vodUtil) {
        VodUtil.ExecuteUpload executeUpload = vodUtil.new ExecuteUpload();
        DefaultAcsClient client;
        RuntimeException runtimeError = null;
        for (int i = 0; i < 4 ; i++) {
            try {
                client = executeUpload.initVodClient(regionId, accessKeyId, accessKeySecret);
                return client;
            } catch (ClientException e) {
                if (i == 3) {
                    runtimeError = new RuntimeException("连接阿里云视频点播服务出错", e);
                } else {
                    log.info("failed: 连接阿里云视频点播服务失败，重试第"+(i+1)+"次；",e.getErrMsg());
                    continue;
                }
            }
        }
        log.error("failed: 无法连接到阿里云视频点播服务；",runtimeError);
        throw runtimeError;
    }

}
